package com.cc.service.impl;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.cc.base.PageResponse;
import com.cc.base.ResponseResult;
import com.cc.enums.HttpsCode;
import com.cc.exception.BusinessException;
import com.cc.mapper.RoleMapper;
import com.cc.pojo.domain.Role;
import com.cc.pojo.dto.PermissionsDto;
import com.cc.pojo.dto.RplePageDto;
import com.cc.pojo.vo.OptionsVo;
import com.cc.service.RoleService;
import jakarta.annotation.Resource;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
@Transactional
public class RoleServiceImpl extends ServiceImpl<RoleMapper, Role> implements RoleService {
    @Resource
    private RoleMapper roleMapper;
    /**
     * 添加角色
     *
     * @param role
     * @return
     */
    @Override
    public ResponseResult<String> addRole(Role role) {
        //1.检查参数
        if (StrUtil.isAllBlank(role.getRoleName(), role.getRoleKey())) {
            throw new BusinessException(HttpsCode.PARAMS_ERROR);
        }
        boolean save = save(role);
        if (!save) {
            //角色添加失败
            throw new BusinessException(HttpsCode.ADD_FAILED);
        }
        return ResponseResult.ok();
    }

    /**
     * 修改角色
     *
     * @param role
     * @return
     */
    @Override
    public ResponseResult<String> updateRole(Role role) {
        //1.检查参数
        if (role.getRoleId() == null) {
            throw new BusinessException(HttpsCode.PARAMS_ERROR, "角色id为空");
        }
        boolean is = updateById(role);
        if (!is) {
            throw new BusinessException(HttpsCode.MODIFICATION_FAILED);
        }
        return ResponseResult.ok();
    }

    /**
     * 角色分页查询
     *
     * @param pageDto
     * @return
     */
    @Override
    public ResponseResult<PageResponse<Role>> rolePage(RplePageDto pageDto) {
        //1.检查参数
        pageDto.checkPage();
        //2.分页查询
        Page<Role> page = page(new Page<>(pageDto.getPageNum(), pageDto.getPageSize()),
                Wrappers.<Role>lambdaQuery()
                        .like(StrUtil.isNotBlank(pageDto.getRoleName()), Role::getRoleName, pageDto.getRoleName())
                        .like(StrUtil.isNotBlank(pageDto.getRoleKey()), Role::getRoleKey, pageDto.getRoleKey())
                        .eq(pageDto.getStatus() != null, Role::getStatus, pageDto.getStatus())
        );
        //3.返回数据
        return ResponseResult.ok(new PageResponse<Role>(page));
    }

    /**
     * 根据角色id获取权限列表id
     * @param roleId
     * @return
     */
    @Override
    public ResponseResult<List<String>> getPermissionsRoleId(Long roleId) {
        //1，检查参数
        if (roleId == null) {
            throw new BusinessException(HttpsCode.PARAMS_ERROR);
        }
        //2，查询权限
        List<String> permissions= roleMapper.getPermissionsRoleId(roleId);
        return ResponseResult.ok(permissions);
    }

    /**
     * 修改角色权限
     *
     * @param permissionsDto
     * @return
     */
    @Override
    public ResponseResult updatePermissions(PermissionsDto permissionsDto) {
        //1.检查参数
        if (permissionsDto.getRoleId() == null) {
            throw new BusinessException(HttpsCode.PARAMS_ERROR);
        }
        //2.更新权限
        //删除角色的所有权限
        roleMapper.deletePermissions(permissionsDto.getRoleId());
        //重新添加角色权限
        if (CollectionUtils.isEmpty(permissionsDto.getPermIds())){
            return ResponseResult.ok();
        }
        roleMapper.insertPermissions(permissionsDto.getRoleId(), permissionsDto.getPermIds());
        return  ResponseResult.ok();
    }

    /**
     * 获取权限权限列表
     * @return
     */
    @Override
    public ResponseResult<List<OptionsVo>> permissionsList() {
        List<Role> list = list();
        if(CollectionUtils.isEmpty(list)){
          return ResponseResult.ok();
        }
        List<OptionsVo> optionsVoList = list.stream().map(role -> new OptionsVo(role.getRoleName(), role.getRoleId().toString())).toList();
        return ResponseResult.ok(optionsVoList);
    }
}
